package com.op.teachingliteapp.fragment.course;

import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.op.teachingliteapp.R;
import com.op.teachingliteapp.bean.CourseBean;
import com.op.teachingliteapp.common.Common;
import com.op.teachingliteapp.database.dao.CourseDao;
import com.op.teachingliteapp.util.SharedPreferencesUtils;
import com.op.teachingliteapp.util.http.VolleySingleton;

public class CourseGridAdapter extends BaseAdapter {

	private List<CourseBean> list;
	private Context mContext;
	private DisplayImageOptions options;
	private String getCourseUserId;

	public CourseGridAdapter(Context mContext) {
		this.mContext = mContext;
		SharedPreferences preferences = mContext.getSharedPreferences("TeachingLite", Context.MODE_PRIVATE);
		getCourseUserId = preferences.getString(Common.Prefs.USER_ID, "");
		// 1. 获得list bean
		refresh();

		// 2. 设置option, 一般默认
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_stub).showImageForEmptyUri(R.drawable.ic_empty).showImageOnFail(R.drawable.ic_error).cacheInMemory(true).cacheOnDisk(true).considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	public Context getContext() {
		return mContext;
	}

	public void refresh() {
		SharedPreferences sharedPreferences = SharedPreferencesUtils.getInstance(getContext());
		String userId = sharedPreferences.getString(Common.Prefs.USER_ID, "");
		CourseDao dao = new CourseDao(getContext(), getCourseUserId);
		list = dao.getCoursesByUserId(userId);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public CourseBean getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public String getCourseId(int position) {
		return list.get(position).getId();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		RelativeLayout fl = null;
		if (convertView != null) {
			fl = (RelativeLayout) convertView;
		} else {
			fl = (RelativeLayout) LayoutInflater.from(getContext()).inflate(R.layout.item_course, parent, false);
		}

		ImageView img = (ImageView) fl.findViewById(R.id.res_img);
		final ProgressBar progressBar = (ProgressBar) fl.findViewById(R.id.progress);
		TextView name_tv = (TextView) fl.findViewById(R.id.res_name);
		name_tv.setText(list.get(position).getName());

		// 3. 传入icon url，加载图片
//		loadImage(list.get(position).getIconUrl(), img, progressBar);
		VolleySingleton.getInstance().loadImage(img, list.get(position).getIconUrl());
		return fl;
	}

	private void loadImage(String imageUrl, ImageView img, final ProgressBar progressBar) {
		ImageLoader.getInstance().displayImage(imageUrl, img, options, new SimpleImageLoadingListener() {
			@Override
			public void onLoadingStarted(String imageUri, View view) {
				progressBar.setProgress(0);
				progressBar.setVisibility(View.VISIBLE);
			}

			@Override
			public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
				progressBar.setVisibility(View.GONE);
			}

			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				progressBar.setVisibility(View.GONE);
			}
		}, new ImageLoadingProgressListener() {
			@Override
			public void onProgressUpdate(String imageUri, View view, int current, int total) {
				progressBar.setProgress(Math.round(100.0f * current / total));
			}
		});
	}

}
